﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using MODEL;
using Microsoft.International.Converters.PinYinConverter;
namespace winUi
{
    public partial class FormDic : Form
    {

        public FormDic()
        {
            InitializeComponent();
        }
        private int editId = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            Dic dic = new Dic();            
            dic.GoodsName = txtName.Text;
            dic.GoodsModel = txtmodel.Text;
            dic.GoodsSpe = txtspe.Text;
            dic.GoodsUnit = txtunit.Text;
            dic.ClassId = (int)cbClass.SelectedValue;
            dic.ClassName = cbClass.Text;
            dic.GoodsPrice = Convert.ToDouble(txtPrice.Text);
            dic.Pingying = txtPY.Text;
            
            
            //MessageBox.Show(dic.Pingying);
            //return;
            try
            {
                int result = 0;
                if (editId > 0)
                {
                    dic.Id = editId;
                    result = new BllDic().Update(dic);
                }
                else
                {
                    if (new BllDic().IsExist(dic) > 0)
                    {
                        MessageBox.Show("该商品已经存在！");
                        return;
                    }
                    result = new BllDic().Insert(dic);
                }
                
                if (result>0&&dic.Id <=0)
                {
                    MessageBox.Show("添加成功");
                }
                else
                {
                    MessageBox.Show("编辑成功");
                }
                LoadData();                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
           
        }
        void LoadData()
        {            
            dgvDic.DataSource = new BllDic().GetByPY("");
            dgvDic.FirstDisplayedScrollingRowIndex = dgvDic.RowCount - 1;
            dgvDic.Rows[dgvDic.RowCount - 1].Selected = true;
        }
        private void FormDic_Load(object sender, EventArgs e)
        {
            cbClass.DisplayMember = "ClassName";
            cbClass.ValueMember = "Id";
            cbClass.DataSource = new BllClass().GetAll();
            cbClass.SelectedIndex = 0;
            dgvDic.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgvDic.MultiSelect = false;
            dgvDic.AllowUserToResizeColumns = false;
            dgvDic.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FormClass fmClass = new FormClass();
            fmClass.ShowDialog();
        }
        public string ToPY(string str)
        {
            StringBuilder sb = new StringBuilder();
            foreach (char c in str)
            {
                if (ChineseChar.IsValidChar(c))
                {
                    ChineseChar cc = new ChineseChar(c);
                    sb.Append(cc.Pinyins[0].Substring(0,1));
                }
                
               
            }
            return sb.ToString();
        }

       
        private void button4_Click(object sender, EventArgs e)
        {
            dgvDic.DataSource = new BllDic().GetByPY(txtKey.Text);

        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvDic.CurrentRow != null)
            {
                DataGridViewRow row = dgvDic.CurrentRow;
                cbClass.SelectedValue = Convert.ToInt32(row.Cells[1].Value.ToString());
                txtName.Text = row.Cells[3].Value.ToString();
                txtmodel.Text = row.Cells[4].Value.ToString();
                txtPrice.Text = row.Cells[7].Value.ToString();
                txtPY.Text = row.Cells[8].Value.ToString();
                txtspe.Text = row.Cells[5].Value.ToString();
                txtunit.Text = row.Cells[6].Value.ToString();
                editId = (int)row.Cells[0].Value;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Clear();
            cbClass.Focus();
        }
        void Clear()
        {
            txtName.Text = "";
            txtmodel.Text = "";
            txtPrice.Text = "0.00";
            txtPY.Text = "";
            txtspe.Text = "";
            txtunit.Text = "";
            cbClass.SelectedIndex = 0;
            editId = 0;
        }

        private void btnCopyAdd_Click(object sender, EventArgs e)
        {
            editId = 0;
        }

        private void txtName_Leave(object sender, EventArgs e)
        {
            txtPY.Text = ToPY(txtName.Text);
        }

        private void cbClass_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtName.Focus();
            }
        }

        private void txtName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtPY.Focus();
            }
        }

        private void txtPY_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtspe.Focus();
            }
        }

        private void txtspe_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtunit.Focus();
            }
        }

        private void txtunit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtmodel.Focus();
            }
        }

        private void txtmodel_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtPrice.Focus();
            }
        }

        private void txtPrice_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSave.Focus();
            }
        }

        private void txtKey_TextChanged(object sender, EventArgs e)
        {
            dgvDic.DataSource = new BllDic().GetByPY(txtKey.Text);
        }    
    }
}
